package com.alotuser;

import cn.hutool.core.io.FileUtil;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.File;

@Mojo(name = "clean-cache")
public class WindowsServiceCleanMojo extends AbstractMojo {
    /**
     * 项目版本号
     */
    @Parameter(property = "project.version", required = true)
    private String version;

    /**
     * 项目artifactId
     */
    @Parameter(property = "project.artifactId", required = true, defaultValue = "")
    private String artifactId;
    /**
     * 输出目录
     */
    @Parameter(property = "project.build.directory", required = true)
    private File targetDir;
    /**
     * 是否需要版本号
     */
    @Parameter(property = "isVersion", defaultValue = "true")
    private boolean isVersion;// 是否需要版本号

    public void execute() {
        getLog().info("清除缓存文件开始");
        String tempDir = System.getProperty("java.io.tmpdir");
        tempDir += "sw";
        String jarPrefixName = getJarPrefixName(isVersion);
        String zipDir = targetDir.getPath() + File.separator + jarPrefixName + ".zip";
        try {
            FileUtil.del(tempDir);
            FileUtil.del(zipDir);
            getLog().info("清除缓存文件成功");
        } catch (Exception e) {
            getLog().error("删除失败", e);
        }
    }


    /**
     * 获取jar包前缀名
     *
     * @param isVersion 是否需要版本号
     * @return String
     */
    private String getJarPrefixName(boolean isVersion) {
        return artifactId + (isVersion ? "-" + version : "");
    }
}
